/*
 [The "BSD licence"]
 Copyright (c) 2017 Sasa Coh, Michał Błotniak
 Copyright (c) 2019 Ivan Kochurkin, kvanttt@gmail.com, Positive Technologies
 Copyright (c) 2019 Dmitry Rassadin, flipparassa@gmail.com, Positive Technologies
 Copyright (c) 2021 Martin Mirchev, mirchevmartin2203@gmail.com
 Copyright (c) 2022 J. Ritchie Carroll, ritchiecarroll@gmail.com
 All rights reserved.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions
 are met:
 1. Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer.
 2. Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in the
    documentation and/or other materials provided with the distribution.
 3. The name of the author may not be used to endorse or promote products
    derived from this software without specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/*
 * A Go grammar for ANTLR 4 derived from the Go Language Specification
 * https://golang.org/ref/spec
 */

parser grammar GoParser;

options {
    tokenVocab = GoLexer;
    superClass = GoParserBase;
}

sourceFile:
    packageClause eos (importDecl eos)* (topLevelDecl eos)* EOF;

// JRC: Current go2cs uses topLevelDecl visitor for conversion work
topLevelDecl:
    declaration | functionDecl | methodDecl;

packageClause: PACKAGE packageName = IDENTIFIER;

importDecl:
    IMPORT (importSpec | L_PAREN (importSpec eos)* R_PAREN);

importSpec: alias = (DOT | IDENTIFIER)? importPath;

importPath: string_;

declaration: constDecl | typeDecl | varDecl;

constDecl: CONST (constSpec | L_PAREN (constSpec eos)* R_PAREN);

constSpec: identifierList (type_? ASSIGN expressionList)?;

identifierList: IDENTIFIER (COMMA IDENTIFIER)*;

expressionList: expression (COMMA expression)*;

typeDecl: TYPE (typeSpec | L_PAREN (typeSpec eos)* R_PAREN);

typeSpec: IDENTIFIER ASSIGN? type_;

// Function declarations

functionDecl: FUNC IDENTIFIER (signature block?);

methodDecl: FUNC receiver IDENTIFIER (signature block?);

receiver: parameters;

varDecl: VAR (varSpec | L_PAREN (varSpec eos)* R_PAREN);

varSpec:
    identifierList (
        type_ (ASSIGN expressionList)?
        | ASSIGN expressionList
    );

block: L_CURLY statementList? R_CURLY;

statementList: (statement eos)+;

statement:
    declaration
    | labeledStmt
    | simpleStmt
    | goStmt
    | returnStmt
    | breakStmt
    | continueStmt
    | gotoStmt
    | fallthroughStmt
    | block
    | ifStmt
    | switchStmt
    | selectStmt
    | forStmt
    | deferStmt;

// JRC: expressionStmt modified to come after sendStmt, otherwise this breaks PointerToPointer behavioral test
simpleStmt:
    sendStmt
    | expressionStmt
    | incDecStmt
    | assignment
    | shortVarDecl
    | emptyStmt;

expressionStmt: expression;

sendStmt: channel = expression RECEIVE expression;

incDecStmt: expression (PLUS_PLUS | MINUS_MINUS);

assignment: expressionList assign_op expressionList;

assign_op: (
        PLUS
        | MINUS
        | OR
        | CARET
        | STAR
        | DIV
        | MOD
        | LSHIFT
        | RSHIFT
        | AMPERSAND
        | BIT_CLEAR
    )? ASSIGN;

shortVarDecl: identifierList DECLARE_ASSIGN expressionList;

emptyStmt: SEMI;

labeledStmt: IDENTIFIER COLON statement?;

returnStmt: RETURN expressionList?;

breakStmt: BREAK IDENTIFIER?;

continueStmt: CONTINUE IDENTIFIER?;

gotoStmt: GOTO IDENTIFIER;

fallthroughStmt: FALLTHROUGH;

deferStmt: DEFER expression;

ifStmt:
    IF (simpleStmt SEMI)? expression block (
        ELSE (ifStmt | block)
    )?;

switchStmt: exprSwitchStmt | typeSwitchStmt;

exprSwitchStmt:
    SWITCH (simpleStmt SEMI)? expression? L_CURLY exprCaseClause* R_CURLY;

exprCaseClause: exprSwitchCase COLON statementList?;

exprSwitchCase: CASE expressionList | DEFAULT;

typeSwitchStmt:
    SWITCH (simpleStmt SEMI)? typeSwitchGuard L_CURLY typeCaseClause* R_CURLY;

typeSwitchGuard: (IDENTIFIER DECLARE_ASSIGN)? primaryExpr DOT L_PAREN TYPE R_PAREN;

typeCaseClause: typeSwitchCase COLON statementList?;

typeSwitchCase: CASE typeList | DEFAULT;

typeList: (type_ | NIL_LIT) (COMMA (type_ | NIL_LIT))*;

selectStmt: SELECT L_CURLY commClause* R_CURLY;

commClause: commCase COLON statementList?;

commCase: CASE (sendStmt | recvStmt) | DEFAULT;

recvStmt: (expressionList ASSIGN | identifierList DECLARE_ASSIGN)? recvExpr = expression;

forStmt: FOR (expression | forClause | rangeClause)? block;

forClause:
    initStmt = simpleStmt? SEMI expression? SEMI postStmt = simpleStmt?;

rangeClause: (
        expressionList ASSIGN
        | identifierList DECLARE_ASSIGN
    )? RANGE expression;

goStmt: GO expression;

type_: typeName | typeLit | L_PAREN type_ R_PAREN;

typeName: qualifiedIdent | IDENTIFIER;

typeLit:
    arrayType
    | structType
    | pointerType
    | functionType
    | interfaceType
    | sliceType
    | mapType
    | channelType;

arrayType: L_BRACKET arrayLength R_BRACKET elementType;

arrayLength: expression;

elementType: type_;

pointerType: STAR type_;

interfaceType:
    INTERFACE L_CURLY ((methodSpec | typeName) eos)* R_CURLY;

sliceType: L_BRACKET R_BRACKET elementType;

// It's possible to replace `type` with more restricted typeLit list and also pay attention to nil maps
mapType: MAP L_BRACKET type_ R_BRACKET elementType;

channelType: (CHAN | CHAN RECEIVE | RECEIVE CHAN) elementType;

methodSpec:
    {noTerminatorAfterParams(2)}? IDENTIFIER parameters result
    | IDENTIFIER parameters;

functionType: FUNC signature;

signature:
    {noTerminatorAfterParams(1)}? parameters result
    | parameters;

result: parameters | type_;

parameters:
    L_PAREN (parameterDecl (COMMA parameterDecl)* COMMA?)? R_PAREN;

parameterDecl: identifierList? ELLIPSIS? type_;

expression:
    primaryExpr
    | unaryExpr
    | expression mul_op = (
        STAR
        | DIV
        | MOD
        | LSHIFT
        | RSHIFT
        | AMPERSAND
        | BIT_CLEAR
    ) expression
    | expression add_op = (PLUS | MINUS | OR | CARET) expression
    | expression rel_op = (
        EQUALS
        | NOT_EQUALS
        | LESS
        | LESS_OR_EQUALS
        | GREATER
        | GREATER_OR_EQUALS
    ) expression
    | expression LOGICAL_AND expression
    | expression LOGICAL_OR expression;

primaryExpr:
    operand
    | conversion
    | methodExpr
    | primaryExpr (
        (DOT IDENTIFIER)
        | index
        | slice_
        | typeAssertion
        | arguments
    );

unaryExpr:
    primaryExpr
    | unary_op = (
        PLUS
        | MINUS
        | EXCLAMATION
        | CARET
        | STAR
        | AMPERSAND
        | RECEIVE
    ) expression;

conversion: type_ L_PAREN expression COMMA? R_PAREN;

operand: literal | operandName | L_PAREN expression R_PAREN;

literal: basicLit | compositeLit | functionLit;

basicLit:
    NIL_LIT
    | integer
    | string_
    | FLOAT_LIT
    | IMAGINARY_LIT
    | RUNE_LIT;

integer:
    DECIMAL_LIT
    | BINARY_LIT
    | OCTAL_LIT
    | HEX_LIT
    | IMAGINARY_LIT
    | RUNE_LIT;

//JRC: Using prior pattern, with code updates this breaks import options behavioral test
operandName: IDENTIFIER | qualifiedIdent; // (DOT IDENTIFIER)?;

qualifiedIdent: IDENTIFIER DOT IDENTIFIER;

compositeLit: literalType literalValue;

literalType:
    structType
    | arrayType
    | L_BRACKET ELLIPSIS R_BRACKET elementType
    | sliceType
    | mapType
    | typeName;

literalValue: L_CURLY (elementList COMMA?)? R_CURLY;

elementList: keyedElement (COMMA keyedElement)*;

keyedElement: (key COLON)? element;

key: IDENTIFIER | expression | literalValue;

element: expression | literalValue;

structType: STRUCT L_CURLY (fieldDecl eos)* R_CURLY;

fieldDecl: (
        {noTerminatorBetween(2)}? identifierList type_
        | anonymousField
    ) tag = string_?;

string_: RAW_STRING_LIT | INTERPRETED_STRING_LIT;

anonymousField: STAR? typeName;

functionLit: FUNC signature block; // function

index: L_BRACKET expression R_BRACKET;

slice_:
    L_BRACKET (
        expression? COLON expression?
        | expression? COLON expression COLON expression
    ) R_BRACKET;

typeAssertion: DOT L_PAREN type_ R_PAREN;

arguments:
    L_PAREN (
        (expressionList | type_ (COMMA expressionList)?) ELLIPSIS? COMMA?
    )? R_PAREN;

methodExpr: receiverType DOT IDENTIFIER;

// JRC: Using original pattern - without code update, using only "type_" currently breaks several tests
receiverType: typeName | '(' ('*' typeName | receiverType) ')';

//receiverType: type_;

eos:
    SEMI
    | EOF
    | {lineTerminatorAhead()}?
    | {checkPreviousTokenText("}")}?
    | {checkPreviousTokenText(")")}?;

